package vasys.mehaniksspacemars;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.block.Biome;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_20_R2.block.CraftBiome;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.java.JavaPlugin;

import net.minecraft.core.registries.Registries;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.resources.ResourceKey;
import vasys.mehaniksspacecore.MehaniksSpaceCore;

public final class MehaniksSpaceMars extends JavaPlugin {

    public List<String> worldSettings = new ArrayList<>();
    Biome mars_valse = null;

    @Override
    public void onEnable() {
        CustomBiomes.regBiomes();

        MehaniksSpaceCore.MehaniksSpaceList.add("MehaniksSpaceMars");
        worldSettings.add("mars"); //short world name
        worldSettings.add("3"); //gravity
        worldSettings.add("-16"); //temperature
        worldSettings.add("55760"); //distance from earth
        worldSettings.add("120"); //block y for liquid water
        MehaniksSpaceCore.MehaniksSpaceWorldMapKeys.add("mehaniks_mars");
        MehaniksSpaceCore.MehaniksSpaceWorldMap.put("mehaniks_mars", worldSettings);

        getServer().getPluginManager().registerEvents(new MarsListener(), this);
        mars_valse = CraftBiome.minecraftToBukkit(CustomBiomes.getBiomeRegistry().a(ResourceKey.a(Registries.ap, new MinecraftKey("mehaniks", "mars_vales"))));

        if (getServer().getWorld("mehaniks_mars") != null) getServer().getWorld("mehaniks_mars").getWorldBorder().setSize(MehaniksSpaceCore.defaultWorldBorderDistance);
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    @Override
    public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
        getLogger().log(Level.WARNING, "MehaniksSpaceMars CustomChunkGenerator is used!");
        return new CustomChunkGenerator(); // Return an instance of the chunk generator we want to use.
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!MehaniksSpaceCore.enabled) {
            sender.sendMessage(ChatColor.RED + "MehaniksSpaceCore is disabled...");
            return true;
        }
        if (command.getName().equalsIgnoreCase("msm")) {
            if (args[0].equals("generate") || args[0].equals("gen")) {
                sender.sendMessage( ChatColor.YELLOW + "Mars began to be generated...");


                WorldCreator wc = new WorldCreator("mehaniks_mars");
                wc.generator("MehaniksSpaceMars");
                wc.environment(World.Environment.NORMAL);
                wc.type(WorldType.NORMAL);
                wc.createWorld();

                getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
                    World world = getServer().getWorld("mehaniks_mars");
                    world.setGameRule(GameRule.DO_WEATHER_CYCLE, false);
                    world.setGameRule(GameRule.DISABLE_RAIDS, true);
                    world.setGameRule(GameRule.DO_PATROL_SPAWNING, false);
                    world.setGameRule(GameRule.DO_TRADER_SPAWNING, false);
                    world.setGameRule(GameRule.COMMAND_BLOCK_OUTPUT, false);
                    world.setDifficulty(Difficulty.HARD);
                    world.setGameRule(GameRule.COMMAND_MODIFICATION_BLOCK_LIMIT, 65536);
                    world.getWorldBorder().setSize(MehaniksSpaceCore.defaultWorldBorderDistance);
                    world.getWorldBorder().setDamageAmount(0);
                    if (MehaniksSpaceCore.useMultiverseCore) {
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mvimport mehaniks_mars normal -g MehaniksSpaceMars");
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mvmset portalForm none mehaniks_mars");
                    }
                    sender.sendMessage(ChatColor.GREEN + "Mars successfully generated!!!");
                });

                return true;
            }
            sender.sendMessage(ChatColor.RED + "msm !null" + mars_valse.getKey());
            return false;
        }
        return false;
    }
}
